package WebserviceChecker;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.hadoop.io.Text;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import DatabaseManaging.JdbcProductsDao;
import DatabaseManaging.ProductDatabaseChecker;
import DatabaseManaging.Models.Product;

public class ProductWbChecker extends WbChecker{
//	WebservReader webReader;

	public ProductWbChecker(){
		super(new ProductDatabaseChecker());
	}
	public ProductWbChecker(ProductDatabaseChecker pdchecker){
		super(pdchecker);
	}
	
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
							//Read Web Service Method //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	@Override
	public HashMap<String, String>  readWebService(String[] record,
			HashMap<String, String> ht)  {
		String stbuffer="";
		ht.put("e",null);
		if("".equals(record[0])){ //Works with Abbs
			ht = this.webReader.readXml(record[1], record[2], "", "products");
			if (ht.get("e") == null) {
				if(!Product.getYear(ht.get("r")).equals("")){
					stbuffer = this.writeProductLine(ht.get("p"), record[1],ht.get("r"),Product.getYear(ht.get("r")),"");
				}else{								
					stbuffer = this.writeProductLine(ht.get("p"), record[1],ht.get("r"),record[2],"");
				}
			} else {
				ht = webReader.readXml(record[1],"all","", "products");
				if(ht.get("e")==null){
					stbuffer =writeProductLine(ht.get("p"), record[1],"all","all",""); //In this case, the webservice does have the product abbreviation stored but doesn't have the release abbreviation stored
				}else{
					ht = webReader.readXml("","all",record[1], "products"); //In this case the baseline web service posseses the abbreviation
					stbuffer = manageBaselineValue(ht, stbuffer,
							record,record[1]);

				}
			}
			
		}else{//Works with baselineValues
			ht = webReader.readXml("","all",record[0], "product");
			stbuffer = manageBaselineValue(ht, stbuffer,
					record,record[0]);
		}
		ht.put("value", stbuffer);
		return ht;
	}
	
	

	
	public String writeProductLine(String nameProduct,String abProduct,String nameVersion,String abVersion,String id){
		generateAbbreviations(abProduct);
		StringBuffer stbuffer=new StringBuffer();
		String product=id+"\t"+nameProduct+"\t"+generateAbbreviations(abProduct);
		if(nameVersion.equals("") && abVersion.equals("")){
			//System.out.println("AUTOCOMPLETE al producto "+nameProduct);
			nameVersion="all";
			abVersion="all";
		}else if(abVersion.equals("") || !(Product.getYear(abVersion).equals(abVersion)) ){
			abVersion=Product.getYear(nameVersion);
		}else if(nameVersion.equals("all")){

			abVersion="all"; 
		}
		String release=nameVersion+"\t"+abVersion+"\t"+""+"\t"+"";
		stbuffer.append(product+release);
		return stbuffer.toString();
	}

	public String generateAbbreviations(String abProduct) {
		String abfinal="";
		String[] abRecord=abProduct.split("\t");
		int i;
		for(i=0;i<abRecord.length;i++){
			abfinal=abfinal+abRecord[i]+"\t";
		}
		for(int g=i;g<5;g++){
			abfinal=abfinal+""+"\t";

		}
		return abfinal;
	}
	
	private String manageBaselineValue(
			HashMap<String, String> ht, String stbuffer, String[] record,String product) {
		if (ht.get("e") == null) {
			if(Product.containsYear(record[2])){
				stbuffer=this.writeProductLine(ht.get("p"),product, record[2], "","");
			}else{
				stbuffer=this.writeProductLine(ht.get("p"),product, "all", "","");
			}
		}else{
			stbuffer=record[1]+"\t"+record[2];
		}
		return stbuffer;
	}
	

	
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
							// CheckNotRepeatedBaseline Methods//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	@Override
	public boolean areEquals(String st1,String st2){
		boolean result=false;
		String [] record1=st1.split("\t");
		String [] record2=st2.split("\t");
		if(record1.length>6 & record2.length>6){			
			if(record1[1].equals(record2[1]) & Product.getYear(record1[7]).equals(Product.getYear(record2[7]))){
				result=true;
			}
		}else{
			if(record1[1].equals(record2[1]) & record1[8].equals(record2[8])){
				//System.out.println("REPEATED!!!!");
				result=true;
			}
		}
		return result;
	}

	@Override
	public String generateReg(String[] recordToAdd, String abbr) {
		String reg="";
		reg=writeProductLine(recordToAdd[1], abbr, recordToAdd[7],recordToAdd[8],"");
		return reg;
	}
	
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	
	public static void main(String args[]){
	    ApplicationContext context = new ClassPathXmlApplicationContext("application-context-persist.xml");
	    JdbcProductsDao dao=(JdbcProductsDao) context.getBean("JdbcProductsDao");
	    ProductDatabaseChecker pdchecker=new ProductDatabaseChecker();
	    pdchecker.setJdbc(dao);
		ProductWbChecker pdChecker=new ProductWbChecker(pdchecker);
		System.out.println("Write the File input");
		try {
		BufferedReader buf=new BufferedReader(new FileReader(args[0]));
		String pattern = buf.readLine();//LOOK AT IT
		ArrayList<Text> list=new ArrayList<Text>();
		while ((pattern = buf.readLine()) != null) {
			String[] rcd=pattern.split("\t",2);
			if("products".equals(rcd[0])){
				System.out.println(rcd[1]);				
				list.add(new Text(rcd[1]));
			}
		}
		buf.close();
		ArrayList<String> errorsList=pdChecker.checkWebService(list);
		System.out.println(errorsList);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


}


